var jsonFile = require('jsonfile')
var fileName = 'ace.json'
const axios = require('axios')
var jsonData = jsonFile.readFileSync(fileName)
const qs = require('qs')

const phone = jsonData.phone
const pwd = jsonData.pwd
const token = jsonData.token
const pay_pwd = jsonData.pay_pwd
const goods = jsonData.goods
var requestArray = []

const instance = axios.create({
  credentials: true,
  baseURL: 'http://ace.acemeta.cc',
  headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36' }
})
instance.defaults.headers['Content-Type'] = 'application/json; charset=utf-8;'
instance.defaults.headers['token'] = token





pushList()


// 商品列表填充
function pushList() {
  setInterval(() => {
    for (let item in goods) {
      instance.get('/api/nft.bazaar/index?page=1&limit=10&category_id=&orderBy=price&sort=desc').then(res => {
        let data = res.data.data.data[0]
        let price = data.price
        let id = data.id
        let name = data.title
        console.log('商品:' + name + '最低价为:' + price + '------价格低于:' + goods[item].price + '时将自动锁定')
        if (price <= goods[item].price) {
          instance.post('/api/nft.bazaar/checkCollection', { bazaar_id: id }).then(res => {
            console.log(res.data)
            if (res.data.code == 1) {
              let token = res.data.data.token
              instance.post('/api/nft.bazaar/addOrder', { order_token: token, bazaar_id: id, num: 1 }).then(res => {
                console.log(res.data)
                if (res.data.code == 1) {
                  axios.post('https://open.feishu.cn/open-apis/bot/v2/hook/9234b14f-e535-41e2-98dd-309f1fd2b580', { "msg_type": "text", "content": { "text": "Ace锁单成功,请上号支付!!!" + price } })
                }
              }).catch(err => {
                console.log(err)
              })
            }
            //   if (res.data.code == 0) {
            //     console.log(res.data.msg)
            //   } else if (res.data.code == 1) {
            //     console.log('锁单成功!')
            //   } else {
            //     console.log(res.data.msg)
            //   }
            // }).catch(err => {
            //   console.log(err.response.data.message)
            // })
          }).catch(err => {
            console.log(err)
          })
        }
      }).catch(err => {
        console.log('频繁')
      })
    }
  }, 50)
}